package com.hyfd.task;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.dahanbank.eif.http.utils.DaHanClientUtil;
import com.hyfd.common.utils.XmlUtils;
import com.hyfd.dao.mp.OrderDao;
import com.hyfd.dao.mp.ProviderPhysicalChannelDao;
import com.hyfd.rabbitMq.RabbitMqProducer;
import com.hyfd.rabbitMq.SerializeUtil;

@Component
public class DHSanTongFlowTask
{
    
    Logger log = Logger.getLogger(getClass());
    
    @Autowired
    OrderDao orderDao;
    
    @Autowired
    RabbitMqProducer mqProducer;// 消息队列生产者
    
    @Autowired
    ProviderPhysicalChannelDao providerPhysicalChannelDao;// 物理通道信息
    
    // @Scheduled(fixedDelay = 60000)
    public void queryDHSanTongOrder()
    {
        Map<String, Object> map = new HashMap<String, Object>();
        int flag = 2;
        try
        {
            String providerId = "00000000";//
            Map<String, Object> channel = providerPhysicalChannelDao.selectByProviderId(providerId);// 获取通道
            String defaultParameter = (String)channel.get("default_parameter");// 参数
            Map<String, String> paramMap = XmlUtils.readXmlToMap(defaultParameter);
            String linkUrl = (String)channel.get("link_url");// 充值地址
            String account = paramMap.get("account");// 平台申请的独立API账号
            String pwd = paramMap.get("pwd");// 平台的appKey
            // 查询订单中属于指通的折扣
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("dispatcherProviderId", providerId);
            param.put("status", "1");
            List<Map<String, Object>> orderList = orderDao.selectByTask(param);// 查询出所有尚通的充值中的订单
            for (int i = 0, size = orderList.size(); i < size; i++)
            {
                Map<String, Object> order = orderList.get(i);
                String orderId = order.get("orderId") + "";// 平台订单号
                
                map.put("orderId", orderId);
                String resMsg = DaHanClientUtil.query(linkUrl, account, pwd, orderId);
                // 判断是否成功
                if (resMsg != null && !resMsg.equals(""))
                {
                    JSONObject resultJson = JSONObject.parseObject(resMsg);// 将返回值转为json对象
                    String status = resultJson.getString("status");// 返回状态码
                    String errorCode = resultJson.getString("errorCode");// 返回信息
                    String errorDesc = resultJson.getString("errorDesc");// 充值状态
                    map.put("resultCode", status + ":" + errorDesc);
                    if (status.equals("0"))
                    {
                        flag = 1;
                    }
                    else if (status.equals("2") || status.equals("3"))
                    {
                        flag = 0;
                    }
                }
                map.put("status", flag);
                mqProducer.sendDataToQueue(RabbitMqProducer.Result_QueueKey, SerializeUtil.getStrFromObj(map));
            }
        }
        catch (Exception e)
        {
            log.error("大汉三通流量查询出错" + e);
        }
    }
}
